/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.Matcher;
import 解析.Pattern;

/**
 *
 * @author yanxi
 */
public class GroupTail extends Node {

    public int localIndex;
    public int groupIndex;

    public GroupTail(int localCount, int groupCount) {
        localIndex = localCount;
        groupIndex = groupCount + groupCount;  
    }
    
    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        int tmp = matcher.locals[localIndex];
        if (tmp >= 0) { // This is the normal group case.
            // Save the group so we can unset it if it
            // backs off of a match.
            int groupStart = matcher.groups[groupIndex];
            int groupEnd = matcher.groups[groupIndex + 1];

            matcher.groups[groupIndex] = tmp;
            matcher.groups[groupIndex + 1] = i;
            if (next.match(matcher, i, seq)) {
                return true;
            }
            matcher.groups[groupIndex] = groupStart;
            matcher.groups[groupIndex + 1] = groupEnd;
            return false;
        } else {
            // This is a group reference case. We don't need to save any
            // group info because it isn't really a group.
            matcher.last = i;
            return true;
        }
    }
    
    @Override
    public void printTree(String s) {
        if(next==Pattern.accept){
            System.out.println(s+toString()+"   next:"+"[ Accept Node ]"); 
        }
        else
            System.out.println(s+toString()+"   next:"+"["+next.getClass().getName()+"]"); 
    }
    
    @Override
    public String toString(){
        return "["+this.getClass().getName()+"]"+"localindex:"+localIndex+",groupIndex:"+groupIndex;
    }
}
